package hh.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import hh.util.coll.CollUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.List;

/**
 * @author : huanghuai
 * @date : 2020/8/4
 */
@Slf4j
public final class DaoUtil implements Serializable {


    private static final long serialVersionUID = 8384892159415083960L;

    private DaoUtil() {

    }

    // query wrapper

    public static <T> QueryWrapper<T> in(String key, List<?> vals) {
        return new QueryWrapper<T>().in(CollUtil.isNotEmpty(vals), key, vals);
    }

    public static <T> QueryWrapper<T> eq(String key, Object val) {
        return new QueryWrapper<T>().eq(CommonUtil.isNotEmpty(val), key, val);
    }

    public static <T> QueryWrapper<T> like(String key, String val) {
        return new QueryWrapper<T>().like(CommonUtil.isNotEmpty(val), key, val);
    }

    public static <T> QueryWrapper<T> eq(String key, Object val, String key2, Object val2) {
        QueryWrapper<T> w = new QueryWrapper<>();
        w.eq(CommonUtil.isNotEmpty(val), key, val);
        w.eq(CommonUtil.isNotEmpty(val2), key2, val2);
        return w;
    }

    public static <T> QueryWrapper<T> eq(String key, Object val, String key2, Object val2, String key3, Object val3) {
        QueryWrapper<T> w = new QueryWrapper<>();
        w.eq(CommonUtil.isNotEmpty(val), key, val);
        w.eq(CommonUtil.isNotEmpty(val2), key2, val2);
        w.eq(CommonUtil.isNotEmpty(val3), key3, val3);
        return w;
    }


    // lambda query wrapper
    public static <T> LambdaQueryWrapper<T> in(SFunction<T, ?> lambdaKey, List<?> vals) {
        return Wrappers.<T>lambdaQuery().in(CollUtil.isNotEmpty(vals), lambdaKey, vals);
    }

    public static <T> LambdaQueryWrapper<T> eq(SFunction<T, ?> lambdaKey, Object val) {
        return Wrappers.<T>lambdaQuery().eq(CommonUtil.isNotEmpty(val), lambdaKey, val);
    }


    public static <T> LambdaQueryWrapper<T> like(SFunction<T, ?> lambdaKey, String val) {
        return Wrappers.<T>lambdaQuery().like(CommonUtil.isNotEmpty(val), lambdaKey, val);
    }

    /**
     * 调用了分页的方法就一定要分页，如果不需要分页，size或size设置-1
     */
    public static void startPage(Integer page, Integer size) {
        if (page == null || size == null
                || page <= 0 || size <= 0) {
            return;
        }
        //PageMethod.startPage(page, size);
    }
}
